package com.holystone.orca.sys.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.holystone.orca.base.service.impl.BaseServiceImpl;
import com.holystone.orca.common.PageData;
import com.holystone.orca.sys.mybatis.dao.SysLookupCdeMapper;
import com.holystone.orca.sys.mybatis.model.SysLookupCde;
import com.holystone.orca.sys.service.ISysLookupCodeService;

@Service(value="sysLookupCodeService")
public class SysLookupCodeServiceImpl extends BaseServiceImpl<SysLookupCde> implements ISysLookupCodeService{

	private static Logger logger = Logger.getLogger(SysLookupCodeServiceImpl.class);
	
	/**dao层服务*/
	@Autowired
	private SysLookupCdeMapper sysLookupCdeMapper;
	
	@Override
	public List<SysLookupCde> getLookupCdeByType(String lookupType){
		logger.debug(">>>getLookupCdeByType(String lookupType)根据码表类型获取码表值");
		Example example = new Example(SysLookupCde.class);
		Criteria cri = example.createCriteria();
		cri.andEqualTo("lookupType", lookupType);
		cri.andNotEqualTo("cdeFlag", 0);
		List<SysLookupCde> listCde =  selectByExample(example);
		logger.debug("码表类型"+lookupType+"值的个数为"+listCde.size()+"<<<getLookupCdeByType(String lookupType)");
		return listCde;
	}


	public PageData pagingSysLookupCde( int iPageNo, int iPageSize,String orderField,String lookupType) {
		logger.debug(">>>pagingSysLookupCde(int iPageNo, int iPageSize,String orderField)分页查询，参数IPageNo="+iPageNo+",iPageSize="+iPageSize);
        Example example = new Example(SysLookupCde.class);
        Criteria cri = example.createCriteria();
        cri.andEqualTo("cdeFlag", 1);
        if(lookupType!=null){
            cri.andEqualTo("lookupType", lookupType);
        }
        example.setOrderByClause(orderField+" desc");
        //分页查询
        PageHelper.startPage(iPageNo, iPageSize);
        PageInfo<SysLookupCde> pageInfo = new PageInfo<SysLookupCde>(sysLookupCdeMapper.selectByExample(example));
		PageData pageData =  new PageData();
		//List<SysLookupCdeVO> listCiType = pageInfo.getList();
		logger.debug("查询的结果集大小"+pageInfo.getList()+"，总页数"+pageInfo.getPages()+"，页大小"+pageInfo.getPageSize()+"，总条数"+pageInfo.getTotal());
		pageData.setPageList(pageInfo.getList());
		pageData.setTotalCount(pageInfo.getTotal());
		pageData.setPageSize(pageInfo.getPageSize());
		pageData.setPageNo(pageInfo.getPageNum());
		pageData.setTotalPage(pageInfo.getPages());
		logger.debug("<<<pagingSysLookupCde(int iPageNO, int iPageSize,String orderField)");
		return pageData;
	}

	@Override
	public boolean addSysLookupCde(SysLookupCde sysLookupCde) {
		logger.debug(">>>addSysLookupCde(SysLookupCde sysLookupCde)添加ci类型，参数sysLookupCde="+sysLookupCde.toString());
		boolean flag = false;
		int i = save(sysLookupCde);
		logger.debug("添加后影响行数 i="+i);
		if(i!=0){
			flag = true;
		}
		logger.debug("<<<addSysLookupCde(SysLookupCde sysLookupCde)返回结果：flag="+flag);
		return flag;
	}

	
	
	public boolean delSysLookupCde(SysLookupCde sysLookupCde) {
		logger.debug(">>>delSysLookupCde(SysLookupCde sysLookupCde)修改资源类型,参数ciType="+sysLookupCde.toString());
		boolean flag = false;
		//int i = delete(sysLookupCde);
		int i = sysLookupCdeMapper.deleteSysLookupCde(sysLookupCde.getLookupCode());
		if(i==1){
			flag=true;
		}
		logger.debug("<<<delSysLookupCde(SysLookupCde sysLookupCde)修改结果："+flag);
		return flag;
	}
	
	@Override
	public boolean editSysLookupCde(SysLookupCde sysLookupCde) {

		logger.debug(">>>editSysLookupCde(SysLookupCde sysLookupCde)修改资源类型,="+sysLookupCde.toString());
		List<SysLookupCde> list = sysLookupCdeMapper.select(sysLookupCde);
		/*
		 * 判断码表类型是否改动，否则code也会随之变化
		 */
		boolean flag = false;
		if(list!=null && list.size()>=1){//查到说明type没变
			int i = updateAll(sysLookupCde);
			if(i==1){
				flag=true;
			}
		}else{
			if(delSysLookupCde(sysLookupCde)){
				flag = addSysLookupCde(sysLookupCde);
			}
		}
		//delSysLookupCde
		logger.debug("<<<editSysLookupCde(SysLookupCde sysLookupCde)修改结果："+flag);
		return flag;
	}

	@Override
	public SysLookupCde selSysLookupCde(SysLookupCde sysLookupCde) {
		List<SysLookupCde> list = sysLookupCdeMapper.select(sysLookupCde);
		if(list!=null && list.size()>=1){
			return list.get(0);
		}
		return null;
	}

	
	@Override
	public SysLookupCde getSysLookupValue(String lookupCode) {
		return sysLookupCdeMapper.selectByPrimaryKey(lookupCode);
	}

    @Override
    public int updateSysLookupFlag(SysLookupCde sysLookupCde) throws Exception {
        logger.debug(">>>updateSysLookupFlag(SysLookupCde sysLookupCde)修改,="+sysLookupCde.toString());
        int result = sysLookupCdeMapper.updateSysLookupFlag(sysLookupCde);
        logger.debug("<<<updateSysLookupFlag(SysLookupCde sysLookupCde)修改结果："+result);
        return result;
    }


	@Override
	public List<SysLookupCde> selectCdeByType(String lookupType)
			throws Exception {
		List<SysLookupCde> lookupCdeList = sysLookupCdeMapper.selectCdeByType(lookupType);
		return lookupCdeList;
	}
}
